import { Behavior } from "./Behavior";
import { Event } from "../../core/event/Event";
import { browser } from "../../core/util/browser";
/**
 *键盘行为的父类
 * @class
 * @extends Behavior
 */
class KeyBoard extends Behavior{
     /**
    *鼠标行为父类构造函数
    * @constructor
    * @param {*} opts 
    */
    static instance=null;
    constructor(opts){
        if(KeyBoard.instance){
            return KeyBoard.instance;
        }
        super(opts);
        KeyBoard.instance=this;
    }
  
    active(){
        document.addEventListener("keydown", this.downHandler=this.down.bind(this));
        document.addEventListener("keyup", this.upHandler=this.up.bind(this));
    }
    deactive(){
        document.removeEventListener("keydown", this.downHandler);
        document.removeEventListener("keyup", this.upHandler);
    }
    down(event){
        event.preventDefault();
        let e=new Event("keydown");
        e.originEvent=event;
        e.data={
            keyCode:event.keyCode,
            shiftKey: event.shiftKey,
            ctrlKey: event.ctrlKey,
            altKey: event.altKey,
            time:browser.now()/1000,
        }
        this.publish(e);
    }
    up(event){
        event.preventDefault();
        let e=new Event("keyup");
        e.originEvent=event;
        e.data={
            keyCode:event.keyCode,
            shiftKey: event.shiftKey,
            ctrlKey: event.ctrlKey,
            altKey: event.altKey,
            time:browser.now()/1000,
        }
        this.publish(e);
    }
    
}
export {KeyBoard}